//
//  BounceView.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/27.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit
//弹窗视图
class PopView : UIView {
    var selectButtonCallBack:((_ title:String)-> Void)?
    
    var contenView:UIView?
    {
        didSet{
            setUpContent()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpContent(){
        
        if self.contenView != nil {
            self.contenView?.frame.origin.y = 0
            self.addSubview(self.contenView!)
        }
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(dismissView)))
        //以下为添加内容，可根据需要删除以下部分
        sudokuConstraints()
    }
    
    @objc func dismissView(){
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
        }) { (true) in
            self.removeFromSuperview()
            self.contenView?.removeFromSuperview()
        }
    }
    
    func showInWindow(){
        UIApplication.shared.keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 1.0
            self.contenView?.frame.origin.y = 0
        }, completion: nil)
    }
    

    //MARK: - 布局
    func sudokuConstraints(){
       let layerView = UIView()
        layerView.frame = CGRect(x: (screenWidth-300)/375/2*screenWidth, y: (218.5/667)*screenHeight, width: (300/375)*screenWidth, height: (180/375)*screenWidth)
       layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
       let layer = CALayer()
       layer.frame = layerView.bounds
       layer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
       layerView.layer.addSublayer(layer)
       layerView.layer.cornerRadius = 10;
        layerView.layer.masksToBounds = true
        self.contenView?.addSubview(layerView)
        
        addEnterButton(layerView)
        addPasterButton(layerView)
   }
    
    func addEnterButton(_ view:UIView){
        //确定按钮
        let button1 = UIButton()
        button1.frame = CGRect(x: (18/375)*screenWidth, y: (124/375)*screenWidth, width: 125, height: 40)
        button1.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer1 = CALayer()
        layer1.frame = button1.bounds
        layer1.backgroundColor = UIColor(red: 0.92, green: 0.92, blue: 0.92, alpha: 1).cgColor
        button1.layer.addSublayer(layer1)
        button1.layer.cornerRadius = 20;
        button1.layer.masksToBounds = true
        button1.setTitle("确定", for: .normal)
        button1.setTitleColor(UIColor(red: 102, green: 102, blue: 102, alpha: 102), for: .normal)
        button1.titleLabel?.font = .systemFont(ofSize: 14)
        view.addSubview(button1)
    }
    
    func  addPasterButton(_ view:UIView){
        let layerView = UIButton()
        layerView.frame = CGRect(x: (157/375)*screenWidth, y: (124/375)*screenWidth, width: 125, height: 40)
        // layerFillCode
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        // gradientCode
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 0.67, green: 0.44, blue: 1, alpha: 1).cgColor, UIColor(red: 0.44, green: 0.33, blue: 1, alpha: 1).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
        gradient1.frame = layerView.bounds
        layerView.layer.addSublayer(gradient1)
        layerView.layer.cornerRadius = 20;
        layerView.layer.masksToBounds = true
        layerView.setTitle("去微信粘贴", for: .normal)
        layerView.setTitleColor(UIColor.white, for: .normal)
        layerView.titleLabel?.font = .systemFont(ofSize: 14)
        view.addSubview(layerView)
    }
}
